﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace MT.ValidationRules
{
    public class IdCardValidationRules : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()))
                return new ValidationResult(true, "OK");
            var str = value.ToString();
            if (!IsIdentityCard(str))
            {
                return new ValidationResult(false, "请输入正确的身份证号");
            }
            return new ValidationResult(true, "OK");
        }
        /// <summary>
        /// 验证身份证
        /// </summary>        
        public static bool IsIdentityCard(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) 
                return false;

            Regex objReg = new Regex(@"^(^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase);
            return objReg.IsMatch(value);
        }
    }
}
